package com.jboa.web.action;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jboa.service.IDeptService;
import com.jboa.service.IEmployeeService;
import com.jboa.service.IPositionService;
import com.jboa.vo.SysDepartment;
import com.jboa.vo.SysEmployee;
import com.jboa.vo.SysPosition;
import com.opensymphony.xwork2.ActionContext;

public class EmployeeAction {
	private String number;
	private String password;
	private SysEmployee employee;
	private List<SysDepartment> departmentLs;
	private List<SysPosition> positionLs;
	@Autowired
	@Qualifier("employeeService")
	private IEmployeeService service;
	@Autowired
	@Qualifier("deptService")
	private IDeptService deptservice;
	@Autowired
	@Qualifier("positionService")
	private IPositionService positionservice;
	
	//登录
	public String login() throws Exception{
		SysEmployee employee=new SysEmployee();
		employee.setSn(number);
		employee.setPassword(password);
		SysEmployee dbEmployee=service.login(employee);
		//用户不存在
		if(dbEmployee==null){
			System.out.println("用户不存在");
			return "input";
		}
		//密码正确
		if(dbEmployee.getPassword().equals(employee.getPassword())){
			//设置员工信息到session
			Map<String,Object> session=ActionContext.getContext().getSession();
			session.put("employee", dbEmployee);
			System.out.println(dbEmployee.getSysDepartment());
			//设置员工的职位信息到session
			getPositionToSession(session, dbEmployee.getSysPosition());
			return "success";
		}
		//密码错误
		System.out.println("密码错误");
		return "input";
	}

	//设置员工的职位信息到session
	private void getPositionToSession(Map<String, Object> session, SysPosition position) {
		String nameCn=position.getNameCn();
		if(nameCn.equals("员工")){
			session.put("employee_position", "员工");
		}else if(nameCn.equals("部门经理")){
			session.put("employee_position", "部门经理");
		}else if(nameCn.equals("总经理")){
			session.put("employee_position", "总经理");
		}else if(nameCn.equals("财务")){
			session.put("employee_position", "财务");
		}
	}
	
	/**
	 * 注册
	 * @return
	 */
	public String register(){
		try {
			service.register(employee);
			//设置员工的信息到session
			Map<String,Object> session=ActionContext.getContext().getSession();
			session.put("employee", employee);
			employee.setSysPosition(positionservice.getByid(employee.getSysPosition().getId()));
			//设置员工的职位信息到session
			getPositionToSession(session, employee.getSysPosition());
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("注册出错2");
			return "input";
		}
		
	}
	
	/**
	 * 获取注册页面需要的信息
	 * @return
	 */
	public String getRegisterInfo(){
		departmentLs=deptservice.getAll();
		positionLs=positionservice.getAll();
		return "success";
	}
	
	public SysEmployee getEmployee() {
		return employee;
	}

	public void setEmployee(SysEmployee employee) {
		this.employee = employee;
	}

	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public List<SysDepartment> getDepartmentLs() {
		return departmentLs;
	}

	public void setDepartmentLs(List<SysDepartment> departmentLs) {
		this.departmentLs = departmentLs;
	}

	public List<SysPosition> getPositionLs() {
		return positionLs;
	}

	public void setPositionLs(List<SysPosition> positionLs) {
		this.positionLs = positionLs;
	}

}
